package org.nfyszny.zzzs.modules.service.impl;

import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.system.mapper.SysLogMapper;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecg.modules.vo.OperateReportVo;
import org.nfyszny.zzzs.modules.mapper.ZzzsExamineMapper;
import org.nfyszny.zzzs.modules.service.IZzzsReportService;
import org.nfyszny.zzzs.modules.utils.UserUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ZzzsReportServiceImpl implements IZzzsReportService {
    @Autowired
    private SysLogMapper logMapper;
    @Autowired
    private ISysDepartService departService;
    @Autowired
    private ZzzsExamineMapper examineMapper;

    @Override
    public List<OperateReportVo> operateReport(String year) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy");

        LoginUser userInfo = UserUtils.getUserInfo();
        List<String> list =null;
        if(userInfo!=null){
        list = departService.selectLowerCode(userInfo.getId());
        }
        List<OperateReportVo> operateReportVos = new ArrayList<>();

        Date parse = new Date();
        //判断年限传递
        if (!StringUtils.isEmpty(year)) {
            year = year.replace('"', ' ');
            parse = simpleDateFormat.parse(year);

        }
        //查询登录次数
        List<org.jeecg.modules.vo.OperateReportVo> countYear = logMapper.loginCountYear(list, parse);
        //查询操作次数
        List<org.jeecg.modules.vo.OperateReportVo> operateCountYear = logMapper.operateCountYear(list, parse);
        List<OperateReportVo> examineCountYear = examineMapper.examineCountYear(list, parse, userInfo==null?null:userInfo.getId());
        List<OperateReportVo> errorCountYear = examineMapper.examineErrorCountYear(list, parse, userInfo==null?null:userInfo.getId());
        for (int i = 0; i < countYear.size(); i++) {
            OperateReportVo operateReportVo = new OperateReportVo();

            operateReportVo.setLoginCount(countYear.get(i).getLoginCount() == null ? 0 : countYear.get(i).getLoginCount());
            if (operateCountYear.size() - 1 >= i && operateCountYear.size() != 0) {
                operateReportVo.setOperateCount(operateCountYear.get(i).getOperateCount() == null ? 0 : operateCountYear.get(i).getOperateCount());
            }else {
                operateReportVo.setOperateCount(0);
            }
            if (examineCountYear.size() - 1 >= i && examineCountYear.size() != 0) {
                operateReportVo.setCheckCount(examineCountYear.get(i).getCheckCount() == null ? 0 : examineCountYear.get(i).getCheckCount());
            } else {
                operateReportVo.setCheckCount(0);
            }
            if (errorCountYear.size() - 1 >= i && errorCountYear.size() != 0) {
                operateReportVo.setErrorCount(errorCountYear.get(i).getErrorCount() == null ? 0 : errorCountYear.get(i).getErrorCount());
            } else {
                operateReportVo.setErrorCount(0);
            }
            operateReportVo.setType(countYear.get(i).getType());
            operateReportVos.add(operateReportVo);
        }
        System.out.println("operateReportVos = " + operateReportVos);
        return operateReportVos;
    }

    @Override
    public List<OperateReportVo> operateQuarteReport(String quarte) {
        LoginUser userInfo = UserUtils.getUserInfo();
        List<String> list =null;
                if(userInfo!=null) {
                   list = departService.selectLowerCode(userInfo.getId());
                }
        List<org.jeecg.modules.vo.OperateReportVo> countQuarte = logMapper.loginCountQuarte(list, quarte);

        List<OperateReportVo> operateReportVos = new ArrayList<>();
        List<org.jeecg.modules.vo.OperateReportVo> operateCountQuarte = logMapper.operateCountQuarte(list, quarte);
        List<OperateReportVo> examineCountQuarte = examineMapper.examineCountQuarte(list, quarte, userInfo==null?null:userInfo.getId());
        List<OperateReportVo> errorCountYear = examineMapper.examineErrorCountQuarte(list, quarte, userInfo==null?null:userInfo.getId());
        for (int i = 0; i < countQuarte.size(); i++) {
            OperateReportVo operateReportVo = new OperateReportVo();

            operateReportVo.setLoginCount(countQuarte.get(i).getLoginCount() == null ? 0 : countQuarte.get(i).getLoginCount());

            if (operateCountQuarte.size() - 1 >= i && operateCountQuarte.size() != 0) {
                operateReportVo.setOperateCount(operateCountQuarte.get(i).getOperateCount() == null ? 0 : operateCountQuarte.get(i).getOperateCount());
            }else {
                operateReportVo.setOperateCount(0);
            }
            if (examineCountQuarte.size() != 0 && examineCountQuarte.size() - 1 >= i) {
                operateReportVo.setCheckCount(examineCountQuarte.get(i).getCheckCount() == null ? 0 : examineCountQuarte.get(i).getCheckCount());
            } else {
                operateReportVo.setCheckCount(0);
            }
            if (errorCountYear.size() != 0 && errorCountYear.size() - 1 >= i) {
                System.out.println("examineCountQuarte = " + examineCountQuarte);
                operateReportVo.setErrorCount(errorCountYear.get(i).getErrorCount() == null ? 0 : errorCountYear.get(i).getErrorCount());
            } else {
                operateReportVo.setErrorCount(0);
            }
            operateReportVo.setType(countQuarte.get(i).getType());
            operateReportVos.add(operateReportVo);
        }

        return operateReportVos;


    }

    @Override
    public List<OperateReportVo> operateMonthReport(String month) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");

        LoginUser userInfo = UserUtils.getUserInfo();

        List<String> list = null;
        if(userInfo!=null) {
            list = departService.selectLowerCode(userInfo.getId());
        }
        List<OperateReportVo> operateReportVos = new ArrayList<>();

        Date parse = new Date();
        //判断年限传递
        if (!StringUtils.isEmpty(month)) {
            month = month.replace('"', ' ');
            parse = simpleDateFormat.parse(month);

        }
        //查询登录次数
        List<org.jeecg.modules.vo.OperateReportVo> countYear = logMapper.loginCountMonth(list, parse);
        //查询操作次数
        List<org.jeecg.modules.vo.OperateReportVo> operateCountYear = logMapper.operateCountMonth(list, parse);
        List<OperateReportVo> examineCountYear = examineMapper.examineCountMonth(list, parse, userInfo==null?null:userInfo.getId());
        List<OperateReportVo> errorCountYear = examineMapper.examineErrorCountMonth(list, parse, userInfo==null?null:userInfo.getId());
        for (int i = 0; i < countYear.size(); i++) {
            OperateReportVo operateReportVo = new OperateReportVo();

            operateReportVo.setLoginCount(countYear.get(i).getLoginCount() == null ? 0 : countYear.get(i).getLoginCount());


            if (operateCountYear.size() - 1 >= i && operateCountYear.size() != 0) {
                operateReportVo.setOperateCount(operateCountYear.get(i).getOperateCount() == null ? 0 : operateCountYear.get(i).getOperateCount());
            }else {
                operateReportVo.setOperateCount(0);
            }
            if (examineCountYear.size() - 1 >= i && examineCountYear.size() != 0) {
                operateReportVo.setCheckCount(examineCountYear.get(i).getCheckCount() == null ? 0 : examineCountYear.get(i).getCheckCount());
            } else {
                operateReportVo.setCheckCount(0);
            }
            if (errorCountYear.size() - 1 >= i && examineCountYear.size() != 0) {
                operateReportVo.setErrorCount(errorCountYear.get(i).getErrorCount() == null ? 0 : errorCountYear.get(i).getErrorCount());
            } else {
                operateReportVo.setErrorCount(0);
            }
            operateReportVo.setType(countYear.get(i).getType());
            operateReportVos.add(operateReportVo);
        }

        return operateReportVos;
    }
}
